package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.service.GarmentStyleService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.GarmentStyleForm;
import com.youlai.boot.factory.model.query.GarmentStyleQuery;
import com.youlai.boot.factory.model.vo.GarmentStyleVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 服装款式前端控制层
 *
 * @author youlaitech
 * @since 2025-07-06 16:02
 */
@Tag(name = "服装款式接口")
@RestController
@RequestMapping("/api/v1/garment-style")
@RequiredArgsConstructor
public class GarmentStyleController {

    private final GarmentStyleService garmentStyleService;

    @Operation(summary = "服装款式分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:garment-style:query')")
    public PageResult<GarmentStyleVO> getGarmentStylePage(GarmentStyleQuery queryParams) {
        IPage<GarmentStyleVO> result = garmentStyleService.getGarmentStylePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "服装款式联客户表分页列表")
    @GetMapping("/joinPage")
    @PreAuthorize("@ss.hasPerm('factory:garment-style:query')")
    public PageResult<GarmentStyleVO> getGarmentStyleWithCustomer(GarmentStyleQuery queryParams) {
        IPage<GarmentStyleVO> result = garmentStyleService.getGarmentStyleWithCustomer(queryParams);
        return PageResult.success(result);
    }


    @Operation(summary = "新增服装款式")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:garment-style:add')")
    public Result<Void> saveGarmentStyle(@RequestBody @Valid GarmentStyleForm formData) {
        boolean result = garmentStyleService.saveGarmentStyle(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取服装款式表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:garment-style:edit')")
    public Result<GarmentStyleForm> getGarmentStyleForm(
            @Parameter(description = "服装款式ID") @PathVariable Long id
    ) {
        GarmentStyleForm formData = garmentStyleService.getGarmentStyleFormData(id);
        return Result.success(formData);
    }


    @Operation(summary = "获取服装款式连客户表表单数据")
    @GetMapping("/{id}/joinForm")
    @PreAuthorize("@ss.hasPerm('factory:garment-style:edit')")
    public Result<GarmentStyleVO> getGarmentStyleFormWithCustomer(
            @Parameter(description = "服装款式ID") @PathVariable Long id
    ) {
        GarmentStyleVO formData = garmentStyleService.getGarmentStyleFormWithCustomer(id);
        return Result.success(formData);
    }



    @Operation(summary = "修改服装款式")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:garment-style:edit')")
    public Result<Void> updateGarmentStyle(
            @Parameter(description = "服装款式ID") @PathVariable Long id,
            @RequestBody @Validated GarmentStyleForm formData
    ) {
        boolean result = garmentStyleService.updateGarmentStyle(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除服装款式")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:garment-style:delete')")
    public Result<Void> deleteGarmentStyles(
            @Parameter(description = "服装款式ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = garmentStyleService.deleteGarmentStyles(ids);
        return Result.judge(result);
    }
}
